import java.util.Calendar;

class Time
{	//時間類別
	private int ampm, hour, min;
	String ampmStr;
	
	public Time(int ampm,int hour,int min)
	{
		this.ampm=ampm; this.hour=hour; this.min=min;
	}
	
	public void printDate()
	{
		if (ampm ==1)
			ampmStr = "下午";
		else
			ampmStr = "上午";
			
		System.out.println(ampmStr + hour + "時" + min + "分");
	}
}

class Date
{	//日期類別
	private int year, month,day;
		
	public Date(int year, int month, int day)
	{
		this.year=year; this.month=month; this.day=day;		
	}
	
	public void printDate()
	{
		System.out.print(year + "年" + month + "月" + day + "日");
	}
	
}

class Gentime 
{	//包牌產生日期時間類別
	private Date gendate;
	private Time gentime;
	
	public Gentime(int y, int m, int d,
				   int ampm, int h, int min)
	{
		gendate = new Date(y, m, d);  //建立日期物件
		gentime = new Time(ampm, h, min);  //建立時間物件
	}
	
	public void printGentime()
	{
		System.out.print("產生時間：");
		gendate.printDate();
		gentime.printDate();
	}
}

class Ball
{	//MemberData
	private int initArray[][];	//初始樂透球陣列
	private int xLine;	//樂透球陣列的左索引值
	private int yLine;	//樂透球陣列的右索引值
	private int initTmp=1;
	
	//MemberMethod	
	public int[][] setBall(int x, int y, int max)
	{	//產生樂透球的2維陣列初始值，數字由小到大  
		//setBall(int 左索引值, int 右索引值, int 樂透球最大號碼)
		this.xLine=x;
				
		initArray = new int[xLine][];
		
		for (int i = 0; i < xLine; i++)
		{	
			this.yLine=y;
			initArray[i] = new int[yLine];
		
			for (int j = 0; j < yLine; j++ )
			{
				initArray[i][j] = initTmp;
				initTmp++;
				
				if ( initTmp > max )
					break;
			} //end for(j)
		} //end for(i)
		
		return initArray;  //回傳2維陣列
		
	} //end setInitBall
} //end Ball

class PowerBall extends Ball
{	//MemberData
	private int powerArray[][];
	private int tmpArray[][];
	private int px,py,max;
	String powerStr;
	
	public PowerBall(int x, int y, int max)
	{	//建構子
		this.py=y;
		this.px=x;
		this.max=max;
		//呼叫父類別方法
		tmpArray = setBall(x, y, max);
		//呼叫第1區威力彩包牌號碼
		setLuckyNum();
	}
	
	private void setStubNum( int x, int y, int s)
	{   //產生第7-2組以後的號碼
		//setStubNum(int 即時左索引值, int 即時右索引值, int 殘存值)
		
		for (int l=0; l< this.py-s; l++)
		{			
			powerArray[x][y] = (int) (Math.random()* this.max+1);
					
			for(int i =0; i < y; i++)
			{	//檢查其餘4個號碼不得與同組號碼重複。
				if(powerArray[x][y] == powerArray[x][i])
				{
					powerArray[x][y] = (int) (Math.random()*this.max+1);
					i = 0;
				}				
			} //End for (i)
			
			y++; //powerArray的右索引值+1
		}
	}
	
	private void setLuckyNum()
	{   //產生第1號碼區
		int ranI = (int) (Math.random()*7);
		int ranJ = (int) (Math.random()*6);
		powerArray = new int [px][];
		
		for (int i = 0; i < px; i++ )
		{
			
			powerArray[i] = new int[py];
			
			for (int j = 0; j < py; j++)
			{

				if (i >= 6 && j > 1)
				{	
					setStubNum(i, j, 2);
					break;
				}
				
				while ( tmpArray[ranI][ranJ] == 0)
				{	//如果陣列索引的值等於0，則一直重新產生隨機的陣列索引
					ranI = (int) (Math.random()*7);
					ranJ = (int) (Math.random()*6);
				}
				
				//將隨機產生的初始陣列索引值，依序設定到威力彩陣列
				//並設定該初始陣列索引值為0
				powerArray[i][j] = tmpArray[ranI][ranJ];
				tmpArray[ranI][ranJ] = 0;
				
				ranI = (int) (Math.random()*7);
				ranJ = (int) (Math.random()*6);		
			} //end (j)
		} //end (i)
	} //end setLuckyNum
	
	public String getLucky2()
	{	//產生第2號碼區
		int num;
		String numStr;
		num = (int) (Math.random()*8+1);
		numStr = String.format("%02d", num);
		
		return numStr;
	}
	
	public void printInitBall()
	{	//列印威力彩包牌號碼		
		System.out.println("威力彩小包牌資料如下：");
	    System.out.println("第1號碼區");
	    System.out.println("---------");
		
		for(int i = 1; i <= px; i++)
		{
			System.out.print("第" + i +"組--> ");
			
			for(int j = 0; j < py; j++)
			{
				if (powerArray[i-1][j] <=0)
					break;
				
				powerStr = String.format("%02d", powerArray[i-1][j]);
				System.out.print(powerStr + " ");
			}
			System.out.println("");
		}
		
		System.out.println("");
		System.out.println("第2號碼區");
		System.out.println("---------");
		System.out.println(getLucky2());	
	}
}


public class midex1000412 {
	public static void main(String[] args)
	{
		int x=7; //陣列的左索引值
		int y=6; //陣列的右索引值
		int max=38; //彩球最大值
		
		Calendar ca = Calendar.getInstance();
		int twyear = ca.get(Calendar.YEAR)-1911;
		int m = ca.get(Calendar.MONTH)+1;
		int d = ca.get(Calendar.DATE);
		int ampm = ca.get(Calendar.AM_PM);;
		int h = ca.get(Calendar.HOUR);
		int min = ca.get(Calendar.MINUTE);

		
		PowerBall lot = new PowerBall(x, y, max);  //建立包牌號碼物件		
		Gentime twGentime = new Gentime(twyear, m, d, ampm, h, min); //建立產生日期時間物件		
		twGentime.printGentime(); //列印產生日期時間
		lot.printInitBall(); //輸出威力彩球包牌號碼
	}

}
